﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Collections;

namespace Usas.BLL.Cms
{
    public class StatOperate : System.Web.UI.Page
    {
        private readonly Usas.DAL.Cms.StatOperate so = new Usas.DAL.Cms.StatOperate();

        #region methodOperate.cs
        public bool StatSession()
        {
            return Session["StatSession"] != null ? (Session["StatSession"].ToString() == "1" ? true : false) : false;
        }
        public void StatSessionRemove()
        {
            if (Session["StatSession"] != null)
            {
                Session.Remove("StatSession");
            }
        }
        #region  跟据当前的小时返回相应的字段名
        /// <summary>
        /// 返回时间字段
        /// </summary>
        /// <returns>返回表示当前时间的字段</returns>
        public string getTime()
        {
            string h = " ";
            //获取当前的时间进行判断
            switch (DateTime.Now.Hour)
            {
                case 0: h = "hour0"; break;
                case 1: h = "hour1"; break;
                case 2: h = "hour2"; break;
                case 3: h = "hour3"; break;
                case 4: h = "hour4"; break;
                case 5: h = "hour5"; break;
                case 6: h = "hour6"; break;
                case 7: h = "hour7"; break;
                case 8: h = "hour8"; break;
                case 9: h = "hour9"; break;
                case 10: h = "hour10"; break;
                case 11: h = "hour11"; break;
                case 12: h = "hour12"; break;
                case 13: h = "hour13"; break;
                case 14: h = "hour14"; break;
                case 15: h = "hour15"; break;
                case 16: h = "hour16"; break;
                case 17: h = "hour17"; break;
                case 18: h = "hour18"; break;
                case 19: h = "hour19"; break;
                case 20: h = "hour20"; break;
                case 21: h = "hour21"; break;
                case 22: h = "hour22"; break;
                case 23: h = "hour23"; break;
            }
            return h;
        }
        #endregion

        /// <summary>
        /// 累加日查询条件
        /// </summary>
        /// <returns>字符串变量，累加日查询条件</returns>
        public string strDaySum()
        {
            string str = "hour0";
            for (int i = 1; i < 24; i++)
            {
                str += "+hour" + i;
            }
            return str;
        }
        /// <summary>
        /// 累加日查询条件
        /// </summary>
        /// <returns>字符串变量，累加日查询条件</returns>
        public string strDay()
        {
            string str = "hour0";
            for (int i = 1; i < 24; i++)
            {
                str += ",hour" + i;
            }
            return str;
        }
        /// <summary>
        /// 累加月查询条件
        /// </summary>
        /// <returns>字符串变量，累加月查询条件</returns>
        public string strMonthSum()
        {
            string str = "sum(hour0)";
            for (int i = 1; i < 24; i++)
            {
                str += "+sum(hour" + i + ")";
            }
            return str;
        }
        /// <summary>
        /// 累加月查询条件
        /// </summary>
        /// <returns>字符串变量，累加月查询条件</returns>
        public string strMonth()
        {
            string str = "sum(hour0)";
            for (int i = 1; i < 24; i++)
            {
                str += ",sum(hour" + i + ")";
            }
            return str;
        }

        /// <summary>
        ///返回操作系统信息 
        /// </summary>
        /// <param name="strAgentInfo">传入的浏览器和操作系统信息 </param>
        /// <returns>返回整型变量，1表示windows 2003,2表示windows xp,3表示wiondows 2000,4表示UNIX,5表示Linux</returns>
        public int getSystem(string strAgentInfo)
        {
            DataSet ds = so.getRows("select systemId from tb_system where systemName='" + strAgentInfo + "'");
            if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["systemId"].ToString() != "0")
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0]["systemId"]);
            }
            else
            {
                object obj = so.GetSingle("insert into tb_system values ('" + strAgentInfo + "');select @@IDENTITY");
                if (obj == null)
                {
                    return -1;
                }
                else
                {
                    return Convert.ToInt32(obj);
                }
            }
        }

        /// <summary>
        /// 返回浏览器的信息
        /// </summary>
        /// <param name="strAgentInfo">传入的浏览器和操作系统信息</param>
        /// <returns>返回整型变量，1表示IE6.0浏览器，2表示IE5.0浏览器，3表示FireFox浏览器</returns>
        public int getBrowser(string strAgentInfo)
        {

            DataSet ds = so.getRows("select browserId from tb_browser where browserName='" + strAgentInfo + "'");
            if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["browserId"].ToString() != "")
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0]["browserId"]);
            }
            else
            {
                object obj = so.GetSingle("insert into tb_browser values ('" + strAgentInfo + "');select @@IDENTITY");
                if (obj == null)
                {
                    return -1;
                }
                else
                {
                    return Convert.ToInt32(obj);
                }
            }
        }

        #endregion

        #region DateOperate

        public string GetBrowserVersion(HttpRequest Rq)
        {
            string browsers;
            HttpBrowserCapabilities bc = Rq.Browser;
            string aa = bc.Browser.ToString();
            string bb = bc.Version.ToString();
            browsers = aa + bb;
            return browsers;

        }

        public string GetOSNameByUserAgent(string userAgent)
        {
            string osVersion = "未知";

            if (userAgent.Contains("NT 6.0"))
            {
                osVersion = "Windows Vista/Server 2008";
            }
            else if (userAgent.Contains("NT 5.2"))
            {
                osVersion = "Windows Server 2003";
            }
            else if (userAgent.Contains("NT 5.1"))
            {
                osVersion = "Windows XP";
            }
            else if (userAgent.Contains("NT 5"))
            {
                osVersion = "Windows 2000";
            }
            else if (userAgent.Contains("NT 4"))
            {
                osVersion = "Windows NT4";
            }
            else if (userAgent.Contains("Me"))
            {
                osVersion = "Windows Me";
            }
            else if (userAgent.Contains("98"))
            {
                osVersion = "Windows 98";
            }
            else if (userAgent.Contains("95"))
            {
                osVersion = "Windows 95";
            }
            else if (userAgent.Contains("Mac"))
            {
                osVersion = "Mac";
            }
            else if (userAgent.Contains("Unix"))
            {
                osVersion = "UNIX";
            }
            else if (userAgent.Contains("Linux"))
            {
                osVersion = "Linux";
            }
            else if (userAgent.Contains("SunOS"))
            {
                osVersion = "SunOS";
            }
            return osVersion;
        }


        public bool GlobalStat(string ip, string eid, string years, string months, string days, string browser, string system)
        {
            if (StatSession())
            {
                int systemId = getSystem(system);
                int browserId = getBrowser(browser);
                string sqlIsIP = "select count(*) from tb_dayStat where ip='" + ip + "' and days=" + days + " and  months=" + months + " and years=" + years + " and empid=" + eid;
                string sqlIns = "";
                if (!so.isData(sqlIsIP))
                {
                    //创建SQL语句，插入登录用户的信息
                    sqlIns = "insert into tb_dayStat(IP,empid,sumNum,years,days,months,systemId,browserId) values ('" + ip + "'," + eid + ",1," + years + "," + days + "," + months + "," + systemId + "," + browserId + ")";
                }
                else
                {
                    //创建SQL语句，更新指定用户的访问量加一。
                    sqlIns = "update tb_dayStat set sumNum=sumNum+1 where ip='" + ip + "' and empid=" + eid;
                }
                if (sqlIns.Trim().Length > 0)
                {
                    //执行SQL语句
                    so.exSql(sqlIns);
                    //增加当前时段的访问人数
                    so.insertHour(years, months, days);
                }
            }
            StatSessionRemove();
            return true;
        }

        public DataSet DayStat(string year, string months, string days)
        {
           return so.DayStat(year, months, days, strMonth());
        }

        public object IpStat(string year, string months, string days, string i)
        {
           return so.IpStat(year, months, days, i);
        }
        public DataSet ClientStat(string year, string months, string days)
        {
            return so.ClientStat(year, months, days);
        }
        public DataSet BrowserStat(string year, string months, string days)
        {
            return so.BrowserStat(year, months, days);
        }
        public DataSet ArticleStat(string where, string sqlConn)
        {
            return so.ArticleStat(where, sqlConn);
        }
        public DataSet CmsTypeStat()
        {
            return so.CmsTypeStat();
        }

        public object hourStat(string WHERE)
        {
            return so.hourStat(WHERE);
        }
        #endregion
    }



}
